package com.example.xueal.privatechat;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.InputDevice;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xueal.privatechat.utils.HttpUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;


public class FirstSetActivity extends Activity implements OnClickListener{

    protected static final String TAG = "FirstSetActivity";

    private final String[] mItems = getResources().getStringArray(R.array.Relations);

    private String mNickName = null;
    private String mLuckNum = null;
    private String mRelation = mItems[0];


    @Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.first_set_layout);
		initData();
		initView();
	}
	@Override
	protected void onResume() {
		super.onResume();

	}
	private void initData() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();

	}

	private void initView() {


		// 初始化控件
		Spinner spinner = findViewById(R.id.spn_relation);
		// 建立数据源

		// 建立Adapter并且绑定数据源
		ArrayAdapter<String> adapter=new ArrayAdapter<>(this,android.R.layout.simple_spinner_item, mItems);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		//绑定 Adapter到控件
		spinner .setAdapter(adapter);
		spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
									   int pos, long id) {
                mRelation = mItems[pos];
			}
			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// Another interface callback
			}
		});

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.first_start_btn:
            EditText etNickName = findViewById(R.id.et_nick_name);
            mNickName = etNickName.getText().toString();

            EditText etLuckNum = findViewById(R.id.et_luck_num);
            mLuckNum = etLuckNum.getText().toString();

            String[] params = { mNickName, mLuckNum, mRelation };

            new MyTask().execute(params);

			break;
		default:
			break;
		}
	}

    private class MyTask extends AsyncTask<String, Integer, String> {
        //onPreExecute方法用于在执行后台任务前做一些UI操作
        @Override
        protected void onPreExecute() {
            Log.i(TAG, "onPreExecute() called");
//            textView.setText("loading...");
        }

        //doInBackground方法内部执行后台任务,不可在此方法内修改UI
        @Override
        protected String doInBackground(String... params) {
            Log.i(TAG, "doInBackground(Params... params) called");

            String url = null;
            try {
                url = HttpUtil.BASE_URL
                        + "PrivateChatLoginServlet?nick_name="
                        + URLEncoder.encode(
                        URLEncoder.encode(params[0], "UTF-8"), "UTF-8") + "&luck_num="
                        + URLEncoder.encode(
                        URLEncoder.encode(params[1], "UTF-8"), "UTF-8") + "&relation="
                        + URLEncoder.encode(
                        URLEncoder.encode(params[2], "UTF-8"), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            String result = HttpUtil.queryStringForPost(url);
            return result;
        }

        //onProgressUpdate方法用于更新进度信息
        @Override
        protected void onProgressUpdate(Integer... progresses) {
            Log.i(TAG, "onProgressUpdate(Progress... progresses) called");
        }

        //onPostExecute方法用于在执行完后台任务后更新UI,显示结果
        @Override
        protected void onPostExecute(String result) {
            Log.i(TAG, "onPostExecute(Result result) called");
            if(result.length() == 32){  // uuid
                Intent intent = new Intent(FirstSetActivity.this, MainActivity.class);

                //可以把要传递的参数放到一个bundle里传递过去，bundle可以看做一个特殊的map.
                Bundle bundle=new Bundle();
                bundle.putString("id", result);
                bundle.putString("nick_name", mNickName);
                intent.putExtras(bundle);

                startActivity(intent);
                FirstSetActivity.this.finish();

                Toast.makeText(getApplicationContext(), "登入成功", Toast.LENGTH_SHORT).show();

            }else{
                Toast.makeText(getApplicationContext(), "登入失败", Toast.LENGTH_SHORT).show();
            }
        }

        //onCancelled方法用于在取消执行中的任务时更改UI
        @Override
        protected void onCancelled() {
            Log.i(TAG, "onCancelled() called");
        }
    }

}
